package com.yzqp.common.mvc.httpmessage;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.parser.DefaultJSONParser;
import com.alibaba.fastjson.parser.JSONLexer;
import com.alibaba.fastjson.parser.JSONToken;
import com.alibaba.fastjson.parser.deserializer.ObjectDeserializer;
import com.alibaba.fastjson.serializer.JSONSerializer;
import com.alibaba.fastjson.serializer.ObjectSerializer;
import com.yzqp.common.mvc.base.BaseEnum;

import java.lang.reflect.Type;

/**
 * 该类智能转换body内的数据
 */
public class EnumFastJsonHttpMessageConverter implements ObjectSerializer, ObjectDeserializer {
    /**
     * fastjson 序列化
     *
     * @param serializer
     * @param object
     * @param fieldName
     * @param fieldType
     * @param features
     */
    @Override
    public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) {
        //枚举为空
        if (object == null) {
            serializer.writeNull();
            return;
        }
        //系列化大写的值
        String value = ((BaseEnum) object).getValue();
        String name = ((BaseEnum) object).getName();
        value = value.toUpperCase();
        serializer.write(value);
        //serializer.writeWithFieldName(name, fieldName.toString() + "Name");
        //serializer.writeWithFieldName(name, fieldName.toString() + "Name", String.class, features + 1);
        serializer.getWriter().write(", \n\"" + fieldName + "Name\":  \"" + name + "\"");
    }

    @Override
    public int getFastMatchToken() {
        return JSONToken.LITERAL_STRING;
    }

    /**
     * fastjson 反序列化
     *
     * @param parser
     * @param type
     * @param fieldName
     * @param <T>
     * @return
     */
    @Override
    @SuppressWarnings("unchecked")
    public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
        Class enumType = (Class) type;

        // 类型校验：枚举类型并且实现 EnumValue 接口
        if (!enumType.isEnum() || !BaseEnum.class.isAssignableFrom(enumType)) {
            return null;
        }

        final JSONLexer lexer = parser.lexer;
        final int token = lexer.token();
        Object value = null;
        if (token == JSONToken.LITERAL_INT) {
            value = lexer.integerValue();
        } else if (token == JSONToken.LITERAL_STRING) {
            value = lexer.stringVal();
        } else if (token != JSONToken.NULL) {
            value = parser.parse();
        }

        return (T) BaseEnum.valueOf(enumType, value);
    }

    public static void main(String[] args) {
        String s = "{\"analyze\":\"<p>请编辑内容11111111</p>\",\"answer\":\"11\",\"content\":\"<p>请编辑内容111</p>\",\"contentType\":\"ILLATION\",\"contentTypeName\":\"推理\",\"createTime\":\"2021-01-27 09:56:50\",\"deleteFlag\":\"false\",\"deleteFlagName\":\"否\",\"difficulty\":\"TWO\",\"difficultyName\":\"简单\",\"id\":13,\"questionOptionList\":[{\"content\":\"1\",\"createTime\":\"2021-02-01 09:49:12\",\"deleteFlag\":\"false\",\"deleteFlagName\":\"否\",\"id\":39,\"name\":\"A\",\"questionId\":13},{\"content\":\"2\",\"createTime\":\"2021-02-01 09:49:12\",\"deleteFlag\":\"false\",\"deleteFlagName\":\"否\",\"id\":40,\"name\":\"B\",\"questionId\":13},{\"content\":\"3\",\"createTime\":\"2021-02-01 09:49:12\",\"deleteFlag\":\"false\",\"deleteFlagName\":\"否\",\"id\":41,\"name\":\"C\",\"questionId\":13}],\"questionType\":\"SINGLE_CHOICE\",\"questionTypeName\":\"单选\",\"title\":\"111\"}";
        Object question = JSON.parse(s);
        System.out.println(question);
    }
}
